package it.xiuxian.personcenter.activity

import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import it.xiuxian.lib.base.ArouterAddress
import it.xiuxian.lib.base.BaseActivity
import it.xiuxian.lib.utils.ImageUtil
import it.xiuxian.lib.utils.adapter.SlimAdapter
import it.xiuxian.lib.utils.adapter.SlimInjector
import it.xiuxian.lib.utils.adapter.injector.IViewInjector
import it.xiuxian.personcenter.R
import it.xiuxian.personcenter.bean.GongGaoBean
import it.xiuxian.personcenter.bean.GongGaoList
import it.xiuxian.personcenter.databinding.ActivityJineBinding
import it.xiuxian.personcenter.presenter.GongGaoActivityPresenter
@Route(path = ArouterAddress.GONGGAOACTIVITY)
class GongGaoActivity:BaseActivity<GongGaoActivityPresenter>() {
  lateinit var binding:ActivityJineBinding
   lateinit var adapter:SlimAdapter
    var page=1
    var next=0
    override val layoutId: Int
        get() = R.layout.activity_jine
    override val presenter: GongGaoActivityPresenter
        get() = GongGaoActivityPresenter()

    override fun initView(){
        ARouter.getInstance().inject(this)
      binding =   getViewDataBinding<ActivityJineBinding>()
        binding.rv.layoutManager = LinearLayoutManager(this)
         adapter =SlimAdapter.create().register<GongGaoBean>(R.layout.activity_jine_item, object: SlimInjector<GongGaoBean> {
            override fun onInject(
                    data: GongGaoBean?,
                    injector: IViewInjector<out IViewInjector<*>>?
            ) {

                injector!!.text(R.id.tv_title,data!!.biaoti)
                        .text(R.id.tv_content,data.jianjie)
                        .text(R.id.tv_time,data.createdate)
                        .clicked(R.id.cons, View.OnClickListener {
                            ARouter.getInstance().build(ArouterAddress.GONGGAODETAILACTIVITY).withString("id",data.id.toString()).navigation()
                        })


            }
        }).attachTo(binding.rv)
        binding.smart.setOnLoadMoreListener {
            if(next!=-1){
                getMore()
            }else{
                binding.smart.setNoMoreData(true)
            }
        }
        binding.smart.setOnRefreshListener {
            getRrefresh()
        }
       setTitle("公告列表")
    }
    fun getMore(){
        mPresenter!!.getList((++page).toString())
    }
    fun getRrefresh(){
        page=1
        mPresenter!!.getList((page).toString())
    }
    override fun initData() {
       mPresenter!!.getList("1")
    }
    var newList = ArrayList<GongGaoBean>()
    fun geData(data: GongGaoList){
            if(data.list.isNotEmpty()){
                if(page==1){
                    newList.clear()
                    newList.addAll(data.list)
                }else if(page>1){
                    newList.addAll(data.list)
                }
                next = data.next
                adapter.updateData(newList)
                binding.smart.finishLoadMore()
            }
    }
}